﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class ChiTietPhieuTTKH : MarshalByRefObject
    {

        private static SqlCommand cmd;
        private static ArrayList arr;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.ChiTietPhieuTTCuaKH[] Select(string maphieuTTKH)
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTKH.SelectChiTietPhieuTTKH;
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = maphieuTTKH;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietPhieuTTCuaKH row = new Entities.ChiTietPhieuTTCuaKH();
                    row.MaHDBanHang = (dr[Common.Constants.ChiTietPhieuTTKH.MaHoaDon].ToString());
                    row.MaPhieuTTCuaKH = dr[Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH].ToString();
                    row.TongTienThanhToan = (dr[Common.Constants.ChiTietPhieuTTKH.TongTien].ToString());
                    row.SoNo = (dr[Common.Constants.ChiTietPhieuTTKH.TienNo].ToString());
                    row.ThanhToan = (dr[Common.Constants.ChiTietPhieuTTKH.ThanhToan].ToString());
                    row.GhiChu = dr[Common.Constants.ChiTietPhieuTTKH.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuTTKH.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.ChiTietPhieuTTCuaKH[] arrC = new Entities.ChiTietPhieuTTCuaKH[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietPhieuTTCuaKH)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch
            { return new Entities.ChiTietPhieuTTCuaKH[0]; }

        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.ChiTietPhieuTTCuaKH[] Select()
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTKH.SelectChiTietPhieuTTKH1;

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietPhieuTTCuaKH row = new Entities.ChiTietPhieuTTCuaKH();
                    row.MaHDBanHang = (dr[Common.Constants.ChiTietPhieuTTKH.MaHoaDon].ToString());
                    row.MaPhieuTTCuaKH = dr[Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH].ToString();
                    row.TongTienThanhToan = (dr[Common.Constants.ChiTietPhieuTTKH.TongTien].ToString());
                    row.SoNo = (dr[Common.Constants.ChiTietPhieuTTKH.TienNo].ToString());
                    row.ThanhToan = (dr[Common.Constants.ChiTietPhieuTTKH.ThanhToan].ToString());
                    row.GhiChu = dr[Common.Constants.ChiTietPhieuTTKH.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuTTKH.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.ChiTietPhieuTTCuaKH[] arrC = new Entities.ChiTietPhieuTTCuaKH[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietPhieuTTCuaKH)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch 
            {
               return new Entities.ChiTietPhieuTTCuaKH[0];
                
            }
       

        }

        public static bool InsertUpdate(Entities.ChiTietPhieuTTCuaKH chitietphieuttkh)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.ChiTietPhieuTTKH.InsertUpdate;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.HanhDong, SqlDbType.NVarChar, 50).Value = chitietphieuttkh.HanhDong;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaHoaDon, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaHDBanHang;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaPhieuTTCuaKH;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TongTien, SqlDbType.Float).Value = chitietphieuttkh.TongTienThanhToan;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TienNo, SqlDbType.Float).Value = chitietphieuttkh.SoNo;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.ThanhToan, SqlDbType.Float).Value = chitietphieuttkh.ThanhToan;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.GhiChu, SqlDbType.NVarChar, 200).Value = "";
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.Deleted, SqlDbType.Bit).Value = false;

                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch
            {
                ts.Dispose();
                return false;
            }
        }

        public static bool InsertUpdate(Entities.ChiTietPhieuTTCuaKH[] chitietphieuttkh)
        {
            try
            {
                for (int i = 0; i < chitietphieuttkh.Length; i++)
                {
                    InsertUpdate(chitietphieuttkh[i]);
                }
                return true;
            }
            catch 
            {
                return false;
                
            }
           
        }

    }
}
#endregion